package cn.beemango.dwb.bluetoothheadset.receivers;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.view.KeyEvent;

import cn.beemango.dwb.bluetoothheadset.services.PlayerService;
import cn.beemango.dwb.bluetoothheadset.utils.LogUtil;


/**
 * 当前类注释: 参考 https://github.com/gdoudeng/NetworkMonitor
 * 全局监听网络状态并在注册了接口的activity里面进行对应的操作。
 * 除了网络监听之外，耳机插拔，电量监听等等都可以采取类似的方式进行监听。
 * 包名：com.wteamweb.jjkj.shooting.receivers
 * 作者：Bin on 2020-3-5
 * 邮箱：gdoudeng@qq.com
 * QQ： 1774358245
 * 公司：广州蜂芒网络科技有限公司
 * 站点:<a href="http://www.beemango.cn">www.beemango.cn</a>
 */
public class MediaButtonReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_MEDIA_BUTTON.equals(intent.getAction())) {
            // 获得KeyEvent对象
            final KeyEvent event = intent.getParcelableExtra(Intent.EXTRA_KEY_EVENT);
            // 按下 / 松开
            int keyAction = 0;
            int keyCode = 0;
            if (event != null) {
                // 按下 / 松开
                keyAction = event.getAction();
                // 获得按键码
                keyCode = event.getKeyCode();
            }

            LogUtil.i("MediaButtonReceiver接受到广播 Action ----> " + keyAction + " KeyEvent ----> " + event + " keyCode ----> " + keyCode);
            if (keyAction == KeyEvent.ACTION_DOWN) {
                switch (keyCode) {
                    case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
                        context.startService(new Intent(context, PlayerService.class));
                        break;
                }
            }
        }
    }
}
